define('utils/RichContent', null, () => {
let RichContent = function() {};
RichContent.prototype = {
    constructor: RichContent,
    /**
     * 协议转 html
     */
    protocolToHtml: function(str) {
        // [video src="xxx" poster="xxx"]
        str = str.replace(
            /\[video src="([^"]+)" poster="([^"]+)"\]/gi,
            '<video src="$1" poster="$2" controls="controls"></video>'
        );

        // [img src="xxx"]
        str = str.replace(/\[img src="([^"]+)"\]/gi, '<img src="$1"/>');

        // [link href="xxx"]xxx[/link]
        str = str.replace(
            /\[link href="([^"]+)"\](.*?)\[\/link\]/gi,
            '<a href="$1" target="_blank">$2</a>'
        );

        // [mark data-id="1"]xxx[/mark]
        str = str.replace(/\[mark\s*(?:data\-id="([^"]+)")?\](.*?)\[\/mark\]/gi, (m, p1, p2) => {
            return p1 ? '<mark data-id="'+ p1 +'">'+ p2 +'</mark>' : '<mark>'+ p2 +'</mark>';
        });

        // [p data-role="success"]xxx[/p]
        str = str.replace(/\[p\s*(?:data\-role="([^"]+)")?\](.*?)\[\/p\]/gi, (m, p1, p2) => {
            let con = p2 ? p2 : '<br />';

            return p1 ? '<p data-role="'+ p1 +'">'+ con +'</p>' : '<p>'+ con +'</p>';
        });

        // [b]xxx[/b]
        str = str.replace(/\[b\]/gi, '<b>').replace(/\[\/b\]/gi, '</b>');

        // [blockquote]xxx[/blockquote]
        str = str.replace(/\[blockquote\]/gi, '<blockquote>').replace(/\[\/blockquote\]/gi, '</blockquote>');

        return str;
    },

    htmlToProtocol: function(str) {
        // <video src="xxx" poster="xxx" controls="controls"></video>
        str = str.replace(/<video src="([^"]+)" poster="([^"]+)"[^>]*><\/video>/gi, '[video src="$1" poster="$2"]');

        // <img src="xxx" />
        str = str.replace(/<img src="([^"]+)"[^\/>]*\/?>/gi, '[img src="$1"]');

        // <a href="xxx" target="_blank">xxx</a>
        str = str.replace(/<a href="([^"]+)"[^>]*>(.*?)<\/a>/gi, '[link href="$1"]$2[/link]');

        // <mark data-id="1">xxx</mark>
        str = str.replace(/<mark\s*(?:data\-id="([^"]+)")?>(.*?)<\/mark>/gi, (m, p1, p2) => {
            return p1 ? '[mark data-id="'+ p1 +'"]'+ p2 +'[/mark]' : '[mark]'+ p2 +'[/mark]';
        });

        // <p data-role="xxx">xxx</p>
        str = str.replace(/<p\s*(?:data\-role="([^"]+)")?>(.*?)<\/p>/gi, (m, p1, p2) => {
            return p1 ? '[p data-role="'+ p1 +'"]'+ p2 +'[/p]' : '[p]'+ p2 +'[/p]';
        });

        // <b>xxx</b>
        str = str.replace(/<b>/gi, '[b]').replace(/<\/b>/gi, '[/b]');

        // <blockquote>xxx</blockquote>
        str = str.replace(/<blockquote>/gi, '[blockquote]').replace(/<\/blockquote>/gi, '[/blockquote]');

        return str;
    }
};

return RichContent;
});
